package com.eostoken.eos_wallet.ui.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ArrayUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.eostoken.eos_wallet.MyApplication;
import com.eostoken.eos_wallet.R;
import com.eostoken.eos_wallet.base.BaseFragment;
import com.eostoken.eos_wallet.bean.FindBean;
import com.eostoken.eos_wallet.db.MgpWallet;
import com.eostoken.eos_wallet.mvp.mode.AppsModel;
import com.eostoken.eos_wallet.mvp.presenter.AppsPresenterImpl;
import com.eostoken.eos_wallet.mvp.view.AppsView;
import com.eostoken.eos_wallet.ui.activity.ActivateMidActivity;
import com.eostoken.eos_wallet.ui.activity.MangoAssociationActivity;
import com.eostoken.eos_wallet.ui.activity.MangoNodeActivity;
import com.eostoken.eos_wallet.ui.activity.MiningActivity;
import com.eostoken.eos_wallet.ui.activity.RealTimeDateActivity;
import com.eostoken.eos_wallet.ui.activity.StimulateActivity;
import com.eostoken.eos_wallet.ui.adapter.CustomViewHolder;
import com.eostoken.eos_wallet.utils.ClipboardUtils;
import com.eostoken.eos_wallet.utils.RSAUtils;
import com.eostoken.eos_wallet.utils.WalletUtils;
import com.eostoken.eos_wallet.view.AppsItemView;
import com.ms.banner.Banner;
import com.ms.banner.listener.OnBannerClickListener;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.widget.QMUITopBar;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.eostoken.eos_wallet.utils.Constants.FIND_MID;

public class AppsFragment extends BaseFragment<AppsModel, AppsView, AppsPresenterImpl> implements AppsView {


    @BindView(R.id.topBar)
    QMUITopBar topBar;
    @BindView(R.id.banner)
    Banner banner;


    private static final int DELAYTIME = 3000;
    private static final int id_mango_hashrate = 0;//Mango算力
    private static final int id_decentralized = 1;//去中心化网络 mid绑定
    private static final int id_mango_association = 2;//Mango社群
    private static final int id_mango_node = 3;//Mango节点
    private static final int id_bottom = 4;//底部
    @BindView(R.id.iv_exchange)
    ImageView ivExchange;
    @BindView(R.id.rl_exchange)
    RelativeLayout rlExchange;
    @BindView(R.id.iv_extract)
    ImageView ivExtract;
    @BindView(R.id.rl_extract)
    RelativeLayout rlExtract;
    @BindView(R.id.btnLayout)
    LinearLayout btnLayout;
    @BindView(R.id.mangoHashrateLayout)
    AppsItemView mangoHashrateLayout;
    @BindView(R.id.decentralizedLayout)
    AppsItemView decentralizedLayout;
    @BindView(R.id.mangoAssociationLayout)
    AppsItemView mangoAssociationLayout;
    @BindView(R.id.mangoNodeLayout)
    AppsItemView mangoNodeLayout;
    private List arrList;
    private HashMap<String, Object> mapParams = new HashMap<>();
    private MgpWallet wallet;
    private FindBean findBean;

    private String privateKey;
    private String accountName;
    private String jsonData;
    private String encryptData;
    private int mRadius;

    @Override
    protected int getContentLayout() {
        return R.layout.fragment_apps;
    }

    @Override
    protected void initData() {
        mRadius = QMUIDisplayHelper.dp2px(getActivity(), 8);
        arrList = ArrayUtils.asList(R.mipmap.banner_1, R.mipmap.banner_2, R.mipmap.banner_3, R.mipmap.banner_4);

        updataData();
    }


    private void updataData() {
        wallet = WalletUtils.getCurrentWallet(getActivity());
        privateKey = wallet.getPrivateKey();
        accountName = wallet.getCurrentAccountName();

        mapParams.clear();
        mapParams.put("mgpName", accountName);
        jsonData = GsonUtils.toJson(mapParams);
        try {
            encryptData = RSAUtils.encrypt(jsonData);
            presenter.requestPortData(encryptData, FIND_MID);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void initView(View contentView) {
        topBar.setTitle(getString(R.string.str_apps));
        //简单使用
        banner.setAutoPlay(true)
                .setPages(arrList, new CustomViewHolder())
                .setDelayTime(DELAYTIME)
                .setOnBannerClickListener(new OnBannerClickListener() {
                    @Override
                    public void onBannerClick(List datas, int position) {
//                        Toast.makeText(MainActivity.this, "你点击了：" + position, Toast.LENGTH_SHORT).show();

                    }
                }).start();
        mangoHashrateLayout.setRadius(mRadius);
        decentralizedLayout.setRadius(mRadius);
        mangoAssociationLayout.setRadius(mRadius);
        mangoNodeLayout.setRadius(mRadius);
    }

    @Override
    public AppsModel createModel() {
        return new AppsModel();
    }

    @Override
    public AppsView createView() {
        return this;
    }

    @Override
    public AppsPresenterImpl createPresenter() {
        return new AppsPresenterImpl(getActivity());
    }

    @Override
    public void finishRefresh() {

    }

    @Override
    public void onStart() {
        super.onStart();
        //开始轮播
        if (banner != null && banner.isPrepare() && !banner.isStart()) {
            banner.startAutoPlay();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        updataData();
    }

    @Override
    public void onStop() {
        super.onStop();
        //结束轮播
        if (banner != null && banner.isPrepare() && banner.isStart()) {
            banner.stopAutoPlay();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    @Override
    public void updateView(Object data, int responseSuccessCode) {
        LogUtils.dTag("AppsFragment==", "updateView data = " + data + " responseSuccessCode = " + responseSuccessCode);
        switch (responseSuccessCode) {
            case FIND_MID:
                findBean = GsonUtils.fromJson(data.toString(), FindBean.class);
                if (findBean.getCode() == 0) {
                    FindBean.DataBean dataBean = findBean.getData();
                    MyApplication.getInstance().mMid = dataBean.getLnvitationCode();
                    decentralizedLayout.detailsTitleTv.setText(dataBean.getLnvitationCode());
                    mangoHashrateLayout.setVisibility(View.VISIBLE);
                } else {
                    decentralizedLayout.detailsTitleTv.setText("");
                    mangoHashrateLayout.setVisibility(View.GONE);
                }
                break;
        }
    }

    @Override
    public void onFailure(Object data, int responseFailCode) {

    }

    @Override
    public void updateView(boolean success, Object o1, Object o2) {
    }

    @OnClick({R.id.mangoHashrateLayout, R.id.decentralizedLayout, R.id.mangoAssociationLayout, R.id.mangoNodeLayout, R.id.rl_exchange, R.id.rl_extract})
    public void onViewClicked(View view) {
        String mid = "";
        Intent intent;
        switch (view.getId()) {
            case R.id.rl_exchange:
                ActivityUtils.startActivity(RealTimeDateActivity.class);
                break;
            case R.id.rl_extract:
                mid = decentralizedLayout.detailsTitleTv.getText().toString();
                if (ObjectUtils.isEmpty(mid)) {
                    ToastUtils.showShort(StringUtils.getString(R.string.str_please_active_mid));
                } else {
                    intent = new Intent(getActivity(), StimulateActivity.class);
                    intent.putExtra("isShowState", false);
                    ActivityUtils.startActivity(intent);
                }
                break;
            case R.id.mangoHashrateLayout:
                ActivityUtils.startActivity(MiningActivity.class);
                break;
            case R.id.decentralizedLayout:
                mid = decentralizedLayout.detailsTitleTv.getText().toString();
                if (ObjectUtils.isEmpty(mid)) {
                    ActivityUtils.startActivity(ActivateMidActivity.class);
                } else {
                    ClipboardUtils.copyText(mid);
                    ToastUtils.showShort(StringUtils.getString(R.string.str_copy_success));
                }
                break;
            case R.id.mangoAssociationLayout:
                ActivityUtils.startActivity(MangoAssociationActivity.class);
                break;
            case R.id.mangoNodeLayout:
                ActivityUtils.startActivity(MangoNodeActivity.class);
                break;
        }
    }
}
